package com.xindz.hgq.main;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.xindz.hgq.R;
import com.xindz.hgq.base.BaseActivity;
import com.xindz.hgq.base.options.BaseActivityOptions;
import com.xindz.hgq.utils.view.BottomBar;
import com.xindz.hgq.utils.view.BottomBarTab;

import java.util.zip.Inflater;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MainActivity extends BaseActivity {


    @BindView(R.id.bbt_main)
    BottomBar bbtMain;
    @BindView(R.id.fl_main)
    FrameLayout flMain;
    private Unbinder bind;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private DatabaseFragment dbFragment;

    @Override
    public BaseActivityOptions setOption() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);

        bbtMain.addItem(new BottomBarTab(this, R.drawable.home_tab, "ACTIVITY"))
                .addItem(new BottomBarTab(this, R.drawable.category_tab, "FRAGMENT"))
                .addItem(new BottomBarTab(this, R.drawable.ic_database, "DATABASE"))
                .addItem(new BottomBarTab(this, R.drawable.main_tab, "BASE"));

        bbtMain.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showFragment(position);

            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        showFragment(0);
        SQLiteStudioService.instance().start(this);
    }

    /**
     * 0 oneFragment
     * 1 twoFragment
     * 2 threeFragment
     *
     * @param position
     */
    private void showFragment(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (position) {

            case 0:
                if (twoFragment == null) {
                    twoFragment = new TwoFragment();
                    ft.add(R.id.fl_main, twoFragment);
                }
                hideAllFragment(ft);
                ft.show(twoFragment);
                break;
            case 1:
                if (threeFragment == null) {
                    threeFragment = new ThreeFragment();
                    ft.add(R.id.fl_main, threeFragment);
                }
                hideAllFragment(ft);
                ft.show(threeFragment);

                break;
            case 2:
                if (dbFragment == null) {
                    dbFragment = new DatabaseFragment();
                    ft.add(R.id.fl_main, dbFragment);
                }
                hideAllFragment(ft);

                ft.show(dbFragment);

                break;
            case 3:
                if (oneFragment == null) {
                    oneFragment = new OneFragment();
                    ft.add(R.id.fl_main, oneFragment);
                }
                hideAllFragment(ft);

                ft.show(oneFragment);

                break;
        }
        ft.commit();
    }

    private void hideAllFragment(FragmentTransaction ft) {
        if (oneFragment != null) ft.hide(oneFragment);
        if (twoFragment != null) ft.hide(twoFragment);
        if (threeFragment != null) ft.hide(threeFragment);
        if (dbFragment != null) ft.hide(dbFragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
        SQLiteStudioService.instance().stop();
    }

    /**
     * 解决Fragment重叠问题
     *
     * @param fragment
     */
    @Override
    public void onAttachFragment(android.support.v4.app.Fragment fragment) {
        super.onAttachFragment(fragment);
        if (twoFragment == null && fragment instanceof TwoFragment) {
            twoFragment = (TwoFragment) fragment;
            showFragment(0);
        } else if (threeFragment == null && fragment instanceof ThreeFragment) {
            threeFragment = (ThreeFragment) fragment;
            showFragment(1);
        } else if (dbFragment == null && fragment instanceof DatabaseFragment) {
            dbFragment = (DatabaseFragment) fragment;
            showFragment(2);
        } else if (oneFragment == null && fragment instanceof OneFragment) {
            oneFragment = (OneFragment) fragment;
            showFragment(3);
        }
    }
}
